package org.jeecg.modules.xmss.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DySmsHelper;
import org.jeecg.modules.system.controller.SysOperationLogController;
import org.jeecg.modules.system.entity.SysPermission;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.vo.SysOperationLogPage;
import org.jeecg.modules.xmss.entity.XmssSupervise;
import org.jeecg.modules.xmss.entity.XmssTowns;
import org.jeecg.modules.xmss.mapper.XmssIdeaMapper;
import org.jeecg.modules.xmss.mapper.XmssSuperviseMapper;
import org.jeecg.modules.xmss.service.IXmssSuperviseService;
import org.jeecg.modules.xmss.vo.XmssIdeaPage;
import org.jeecg.modules.xmss.vo.XmssSupervisePage;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * @Description: 意见督办关系表
 * @Author: jeecg-boot
 * @Date: 2020-09-18
 * @Version: V1.0
 */
@Service
public class XmssSuperviseServiceImpl extends ServiceImpl<XmssSuperviseMapper, XmssSupervise> implements IXmssSuperviseService {

    @Autowired
    private XmssSuperviseMapper xmssSuperviseMapper;

    @Autowired
    private SysOperationLogController sysOperationLogController;

    @Autowired
    private IXmssSuperviseService xmssSuperviseService;

    @Autowired

    private XmssIdeaMapper xmssIdeaMapper;

    @Override
    @Transactional
    public void saveMain(XmssSupervise xmssSupervise) {
        xmssSuperviseMapper.insert(xmssSupervise);
    }

    @Override
    @Transactional
    public void updateMain(XmssSupervise xmssSupervise) {
        xmssSuperviseMapper.updateById(xmssSupervise);

        //1.先删除子表数据

        //2.子表数据重新插入
    }

    @Override
    @Transactional
    public void delMain(String id) {
        xmssSuperviseMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            xmssSuperviseMapper.deleteById(id);
        }
    }

    @Override
    public void addSupervise(XmssSupervisePage xmssSupervisePage) {

        List<XmssSupervise> list = new ArrayList<>();
        LambdaQueryWrapper<XmssSupervise> queryWrapper = new LambdaQueryWrapper<XmssSupervise>();
        queryWrapper.eq(XmssSupervise::getIdeaId,xmssSupervisePage.getIdeaId());
        List<XmssSupervise> yczList = this.list(queryWrapper);
        this.duanXin(xmssSupervisePage);
        //先删除
        xmssSuperviseService.delBatchMain(yczList);
        List<String> userIds = yczList.stream().map(XmssSupervise::getUserId).collect(Collectors.toList());

        //添加督办
        if (xmssSupervisePage.getUserId()!=null) {
            xmssSupervisePage.getUserId().stream().forEach(e -> {
               // if (!userIds.contains(e)) {
                    XmssSupervise xmssSupervise = new XmssSupervise();
                    xmssSupervise.setIdeaId(xmssSupervisePage.getIdeaId());
                    xmssSupervise.setUserId(e);
                    list.add(xmssSupervise);
                    SysOperationLogPage getSysOperationLogPage=new SysOperationLogPage();
                    getSysOperationLogPage.setLogContent("办事管理");
                    getSysOperationLogPage.setOperationContent("分配督办"+xmssSuperviseMapper.selectUserId(e).getRealname());
                    sysOperationLogController.add(getSysOperationLogPage);
             //   }
            });
        }

        //添加交办
        List<XmssSupervise> assignUserList = new ArrayList<>();
        List<String> assignUserId = yczList.stream().map(XmssSupervise::getAssignUserId).collect(Collectors.toList());
        if (xmssSupervisePage.getAssignUserId()!=null) {
            xmssSupervisePage.getAssignUserId().stream().forEach(e -> {
              //  if (!assignUserId.contains(e)) {
                    XmssSupervise xmssSupervise = new XmssSupervise();
                    xmssSupervise.setIdeaId(xmssSupervisePage.getIdeaId());
                    //  xmssSupervise.setIdeaId(xmssSupervisePage.getIdeaId());
                    xmssSupervise.setAssignUserId(e);
                    assignUserList.add(xmssSupervise);
                    SysOperationLogPage getSysOperationLogPage=new SysOperationLogPage();
                    getSysOperationLogPage.setLogContent("办事管理");
                    getSysOperationLogPage.setOperationContent("分配交办"+xmssSuperviseMapper.selectUserId(e).getRealname());
                    sysOperationLogController.add(getSysOperationLogPage);
            //    }
            });
        }

        this.saveBatch(list);
        this.saveBatch(assignUserList);
    }

    @Override
    public List<XmssSupervise> supervises(String id) {
        LambdaQueryWrapper<XmssSupervise> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(XmssSupervise::getIdeaId, id);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public void batchDelete(XmssSupervisePage xmssSupervisePage) {
        List<XmssSupervise> getXmssSupervisePageList=new ArrayList<XmssSupervise>();
        XmssSupervise getXmssSupervisePage=new XmssSupervise();
        //去数据库查询是否存在
        LambdaQueryWrapper<XmssSupervise> queryWrapper = new LambdaQueryWrapper<XmssSupervise>();
        queryWrapper.eq(XmssSupervise::getIdeaId,xmssSupervisePage.getIdeaId());
        List<XmssSupervise> yczList = this.list(queryWrapper);
        List<String> userIds = yczList.stream().map(XmssSupervise::getUserId).collect(Collectors.toList());
        List<String> getAssignUserIds = yczList.stream().map(XmssSupervise::getAssignUserId).collect(Collectors.toList());


        //进行删除
      //  if(xmssSupervisePage.getUserId()!=null){
            xmssSupervisePage.getUserId().stream().forEach(e ->{
                if(userIds.contains(e)){
                getXmssSupervisePage.setIdeaId(xmssSupervisePage.getIdeaId());
                getXmssSupervisePage.setUserId(e);
                getXmssSupervisePageList.add(getXmssSupervisePage);
                }
            });
            xmssSuperviseService.delBatchMain(getXmssSupervisePageList);
      //  }

        //进行删除
        List<XmssSupervise> getAssignUserPageList=new ArrayList<XmssSupervise>();
       // if(xmssSupervisePage.getAssignUserId()!=null){
            xmssSupervisePage.getAssignUserId().stream().forEach(e ->{
                if(getAssignUserIds.contains(e)) {
                    getXmssSupervisePage.setIdeaId(xmssSupervisePage.getIdeaId());
                    getXmssSupervisePage.setAssignUserId(e);
                    getAssignUserPageList.add(getXmssSupervisePage);
                    // xmssSuperviseService.updateMain(getXmssSupervisePage);
                }
            });
            xmssSuperviseService.delBatchMain(getAssignUserPageList);
      //  }
    }

    @Override
    public void duanXin(XmssSupervisePage xmssSupervisePage) {


        LambdaQueryWrapper<XmssSupervise> queryWrappers = new LambdaQueryWrapper<XmssSupervise>();
        queryWrappers.eq(XmssSupervise::getIdeaId,xmssSupervisePage.getIdeaId());
        List<XmssSupervise> yczList = this.list(queryWrappers);
        List<String> userIds = yczList.stream().map(XmssSupervise::getUserId).collect(Collectors.toList());
        List<String> AssignUser = yczList.stream().map(XmssSupervise::getAssignUserId).collect(Collectors.toList());


        //发短信给督办
        JSONObject obj = new JSONObject();
       /* LambdaQueryWrapper<XmssSupervise> queryWrapper = new LambdaQueryWrapper<XmssSupervise>();

        queryWrapper.eq(XmssSupervise::getIdeaId,xmssSupervisePage.getIdeaId());*/
       // List<XmssSupervise> yczList = xmssSuperviseService.list(queryWrapper);


        XmssIdeaPage getXmssIdea = new XmssIdeaPage();
        getXmssIdea.setId(xmssSupervisePage.getIdeaId());
        getXmssIdea.setAllot("2");
        getXmssIdea.setCreateTime(new Date());


        if (xmssSupervisePage.getUserId()!=null ) {

            xmssSupervisePage.getUserId().stream().forEach(e -> {
                if(!userIds.contains(e)) {
                    SysUser getUser = xmssSuperviseMapper.selectUserId(e);
                    XmssTowns xmssTowns = xmssIdeaMapper.selectAffair(xmssSupervisePage.getIdeaId());
                    try {
                        if (xmssTowns.getFpTemplateCode() != null) {
                            System.err.println("进来了");
                            DySmsHelper.sendSmsZll(getUser.getPhone(), obj, xmssTowns.getFpTemplateCode(), xmssTowns.getSign());
                        }
                    } catch (ClientException | com.aliyuncs.exceptions.ClientException ex) {
                        ex.printStackTrace();
                    }
                }

            });
        }
        if (xmssSupervisePage.getAssignUserId()!=null ) {
            xmssSupervisePage.getAssignUserId().stream().forEach(e -> {
                if(!AssignUser.contains(e)) {
                    SysUser getUser = xmssSuperviseMapper.selectUserId(e);
                    XmssTowns xmssTowns = xmssIdeaMapper.selectAffair(xmssSupervisePage.getIdeaId());
                    try {
                        if (xmssTowns.getFpTemplateCode() != null) {
                            System.err.println("进来了");
                            DySmsHelper.sendSmsZll(getUser.getPhone(), obj, xmssTowns.getFpTemplateCode(), xmssTowns.getSign());
                        }
                    } catch (ClientException | com.aliyuncs.exceptions.ClientException ex) {
                        ex.printStackTrace();
                    }
                }

            });
        }
    }

}
